Micron Document
🎖️GitЯра🎖️


Displaying Raw • Download

core/testing/src/commonMain/kotlin/org/meshtastic/core/testing/TestScopes.kt 41414f832f938b655c97bc07509bc140ea16e2a3 (41414f83) Text, 2.75 KB

T8b949e/*
* Copyright (c) 2026 Meshtastic LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
Tff7b72package T7ee787org.meshtastic.core.testing

Tff7b72import T7ee787kotlinx.coroutines.CoroutineScope
Tff7b72import T7ee787kotlinx.coroutines.Dispatchers
Tff7b72import T7ee787kotlinx.coroutines.Job
Tff7b72import T7ee787kotlinx.coroutines.cancel
Tff7b72import T7ee787kotlinx.coroutines.delay
Tff7b72import T7ee787kotlinx.coroutines.test.StandardTestDispatcher
Tff7b72import T7ee787kotlinx.coroutines.test.TestScope
Tff7b72import T7ee787kotlinx.coroutines.test.runCurrent
Tff7b72import T7ee787kotlinx.coroutines.test.runTest
Tff7b72import T7ee787kotlinx.coroutines.withContext
Tff7b72import T7ee787kotlin.time.Duration
Tff7b72import T7ee787kotlin.time.Duration.Companion.milliseconds
Tff7b72import T7ee787kotlin.time.Duration.Companion.seconds
Tff7b72import T7ee787kotlin.time.TimeSource

T8b949e/** Runs a test with a structured child scope driven by the test scheduler and always cancels it afterward. */
Tff7b72fun Td2a8ffrunWithRenderScopeTb4b4b4(Te6edf3blockTb4b4b4: Te6edf3suspend Te6edf3TestScopeTb4b4b4.Tb4b4b4(Te6edf3CoroutineScopeTb4b4b4) Tff7b72-Tff7b72> Tffa657UnitTb4b4b4) Tff7b72= Te6edf3runTest Tb4b4b4{
Tff7b72val Te6edf3renderScope Tff7b72= Te6edf3CoroutineScopeTb4b4b4(Te6edf3StandardTestDispatcherTb4b4b4(Te6edf3testSchedulerTb4b4b4) Tff7b72+ Te6edf3JobTb4b4b4(Te6edf3coroutineContextTff7b72[Te6edf3JobTff7b72]Tb4b4b4)Tb4b4b4)
Tff7b72try Tb4b4b4{
Te6edf3blockTb4b4b4(Te6edf3renderScopeTb4b4b4)
Tb4b4b4} Tff7b72finally Tb4b4b4{
Te6edf3renderScopeTb4b4b4.Te6edf3cancelTb4b4b4(Tb4b4b4)
Tb4b4b4}
Tb4b4b4}

Tff7b72private Tff7b72val Te6edf3SETTLE_POLL_INTERVAL Tff7b72= T79c0ff5.Te6edf3milliseconds

T8b949e/**
* Runs the test scheduler until [isSettled] holds, waiting in real time between passes.
*
* Compose resources are loaded on an internal `Dispatchers.Default` scope, so state derived from a string resource
* lands outside the test scheduler and cannot be observed by draining it.
*
* Not safe in a test that asserts on exact virtual time: waiting in real time suspends the test coroutine, and
* `runTest` advances the virtual clock to the next scheduled task whenever that happens, firing pending `delay`s early.
* Such a test should instead load the resources it needs before scheduling anything it later advances past.
*/
Tff7b72suspend Tff7b72fun Te6edf3TestScopeTb4b4b4.Td2a8ffrunUntilSettledTb4b4b4(Te6edf3timeoutTb4b4b4: Te6edf3Duration Tff7b72= T79c0ff1T79c0ff0.Te6edf3secondsTb4b4b4, Te6edf3isSettledTb4b4b4: Tb4b4b4(Tb4b4b4) Tff7b72-Tff7b72> Tffa657BooleanTb4b4b4) Tb4b4b4{
Tff7b72val Te6edf3start Tff7b72= Te6edf3TimeSourceTb4b4b4.Te6edf3MonotonicTb4b4b4.Te6edf3markNowTb4b4b4(Tb4b4b4)
Tff7b72while Tb4b4b4(Tff7b72trueTb4b4b4) Tb4b4b4{
Te6edf3runCurrentTb4b4b4(Tb4b4b4)
Tff7b72if Tb4b4b4(Te6edf3isSettledTb4b4b4(Tb4b4b4)Tb4b4b4) Tff7b72return
Te6edf3checkTb4b4b4(Te6edf3startTb4b4b4.Te6edf3elapsedNowTb4b4b4(Tb4b4b4) Tff7b72< Te6edf3timeoutTb4b4b4) Tb4b4b4{ Ta5d6ff"Ta5d6ffcondition was still not settled after Tffd700$Te6edf3timeoutTa5d6ff" Tb4b4b4}
Te6edf3withContextTb4b4b4(Te6edf3DispatchersTb4b4b4.Te6edf3DefaultTb4b4b4) Tb4b4b4{ Te6edf3delayTb4b4b4(Te6edf3SETTLE_POLL_INTERVALTb4b4b4) Tb4b4b4}
Tb4b4b4}
Tb4b4b4}

Served by rngit 1.5.0 - Generated in 0.04s